package com.mall.order.services;

import com.mall.order.OrderPromoService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.CreateSeckillOrderRequest;
import com.mall.order.dto.CreateSeckillOrderResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.UUID;

/**
 *
 */
@Service
public class OrderPromoServiceImpl implements OrderPromoService {


    @Autowired
    OrderMapper mapper;

    /**
     * 一个生成订单的逻辑直接调用吧
     *
     * @param request
     * @return
     */
// TODO: 2021/12/20 这里需要改成管道模式
    @Override
    public CreateSeckillOrderResponse createPromoOrder(CreateSeckillOrderRequest request) {
        CreateSeckillOrderResponse createSeckillOrderResponse = new CreateSeckillOrderResponse();

        Order order = new Order();
        //orderID
        order.setOrderId(UUID.randomUUID().toString().substring(0, 10));
        BeanUtils.copyProperties(request, order);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setStatus(OrderConstants.ORDER_STATUS_PAYED);
        order.setPayment(request.getPrice());
        order.setPaymentType(1);
        Integer row = mapper.insertSelective(order);
        if (row < 1) {
            createSeckillOrderResponse.setCode(OrderRetCode.DB_SAVE_EXCEPTION.getCode());
            createSeckillOrderResponse.setMsg(OrderRetCode.DB_SAVE_EXCEPTION.getMessage());
            return createSeckillOrderResponse;

        }
        createSeckillOrderResponse.setCode(OrderRetCode.SUCCESS.getCode());
        createSeckillOrderResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        return createSeckillOrderResponse;

    }
}
